/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.geom.Point;
import org.aswing.geom.Rectangle;
import org.aswing.JFrame;

import com.bourre.events.EventType;

import fever.app.local.LocalisationEvent;
import fever.display.ModalScreen;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

import fvaswing.components.datechooser.FvCalendarListener;
import fvaswing.components.datechooser.FvCalendarResources;
import fvaswing.components.FvCalendar;
import fvaswing.FvAsWing;

/**
 * {@code FvCalendarChooser } class.
 *
 * @author Romain Ecarnot
 */
class fvaswing.components.datechooser.FvCalendarChooser
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _instance : FvCalendarChooser;
	
	private var _modal : ModalScreen;
	private var _modalTarget : MovieClip;
	private var _frame : JFrame;
	private var _calendar : FvCalendar;	private var _listener : FvCalendarListener;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	public static function getInstance() : FvCalendarChooser
	{
		if( !_instance ) _instance = new FvCalendarChooser();
		return _instance;	
	}
	
	/**
	 * Sets max year value.
	 */
	public function setMaximumYear( year : Number ) : Void
	{
		_calendar.setMaximumYear( year );
	}
	
	/**
	 * Returns max year value.
	 */
	public function getMaximumYear() : Number
	{
		return _calendar.getMaximumYear();	
	}
	
	/**
	 * Sets min year value.
	 */
	public function setMinimumYear( year : Number ) : Void
	{
		_calendar.setMinimumYear( year );	
	}
	
	/**
	 * Returns min year value.
	 */
	public function getMinimumYear() : Number
	{
		return _calendar.getMinimumYear();	
	}
	
	/**
	 * Indicates if component highlight 'today' cell.<br />
	 * Default is {@code true}
	 */
	public function setShowTodayEnabled( b : Boolean ) : Void
	{
		_calendar.setShowTodayEnabled( b );
	}
	
	/**
	 * Returns {@code true} if calendar highlight the 'today' cell.
	 */
	public function isShowTodayEnabled() : Boolean
	{
		return _calendar.isShowTodayEnabled();	
	}
	
	/**
	 * Sets the first day ( index ) of a week.<br />
	 * Default is 
	 */
	public function setFirstDayOfTheWeek( n : Number ) : Void
	{
		_calendar.setFirstDayOfTheWeek( n );
	}
	
	/**
	 * Sets selected date.
	 */
	public function setSelectedDate( date : Date ) : Void
	{
		_calendar.setSelectedDate( date );
	}
	
	/**
	 * Opens chooser.
	 * 
	 * <p>Be sure to call {@link #connect()} method before to define good result handler.
	 */
	public function open() : Void
	{
		if( !_listener )
		{
			FeverDebug.WARN( 'No listener defined for calendar chooser.' );
			return;
		}
		
		_layout();
		
		_modal.show();
		_frame.show();
	}
	
	/**
	 * Connects a {@link fvaswing.components.datechooser.FvCalendarListener} instance 
	 * to receive calendar result.
	 */
	public function connect( listener : FvCalendarListener ) : Void
	{
		_listener = listener;
	}
	
	/**
	 * Sets dialog location.
	 */
	public function setLocation( p : Point ) : Void
	{
		_frame.setLocation( p );
	}
	
	/**
	 * Triggered when Localisation language change.
	 */
	public function onLocalisationUpdate(event : LocalisationEvent ) : Void
	{
		super.onLocalisationUpdate( event );
		
		_frame.setTitle( FvCalendarResources.getInstance().title );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvCalendarChooser()
	{
		super();
		
		_configureUI();
		_configureEvents();
	}
	
	private function _getModalTarget() : MovieClip
	{
		_modal.dispose();
		_modal = new ModalScreen( 0x000000, 0 );
		
		return _modal.getModalTarget();
	}
	
	private function _configureUI() : Void
	{
		_calendar = new FvCalendar();
		
		_frame = new JFrame( _getModalTarget(), FvCalendarResources.getInstance().title, false );
		_frame.setContentPane( _calendar );
		_frame.setResizable( false );		_frame.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE );
		
		_frame.setSize( 200, 200 );
	}
	
	private function _configureEvents() : Void
	{
		_calendar.addEventListener( FvCalendar.onCalendarClickEVENT, _handleResult, this );
		_frame.addEventListener( JFrame.ON_WINDOW_CLOSED,_handleClose, this );
	}
	
	private function _handleResult( source : FvCalendar, type : EventType ) : Void
	{
		_listener[ type ]( source.getSelectedDate() );
		
		_handleClose();
	}
	
	private function _handleClose() : Void
	{
		_modal.hide();
		_frame.tryToClose();	
	}
	
	private function _layout() : Void
	{
		var frameBounds : Rectangle = _frame.getBounds();
		var stageBounds : Rectangle = FvAsWing.getInstance().getContainer().getBounds();
		
		var newX : Number = _frame.getX();
		var newY : Number = _frame.getY();
		var diff : Number = frameBounds.x - stageBounds.x;
		
		if( diff < 0 )	newX = stageBounds.x;
		else
		{
			diff = stageBounds.getRightTop().x - frameBounds.getRightTop().x;
			if( diff < 0 ) newX = stageBounds.getRightTop().x - frameBounds.width; 
		}  
		
		diff = frameBounds.y - stageBounds.y;
		if( diff < 0 ) newY = stageBounds.y;
		else
		{
			diff = stageBounds.getRightBottom().y - frameBounds.getRightBottom().y;
			if( diff < 0 ) newY = stageBounds.getRightBottom().y - frameBounds.height; 
		}
		
		_frame.setLocation( newX, newY );
		 
	}
}